function setupPageAffix() {
  var nextStatus = $('.me-affix input[name=next-status]').val();
  if (nextStatus == "") {
    $('#page-side').addClass('col-md-3 col-lg-3').removeClass('hidden');
    $('#page-main').addClass('col-md-9 col-lg-9').removeClass('col-md-12 col-lg-12');
    $('.me-affix button').attr('title', '隐藏边栏');
    $('.me-affix input[name=next-status]').val('hidden');
  } else {           // nextStatus == "hidden"
    $('#page-side').removeClass('col-md-3 col-lg-3').addClass('hidden');
    $('#page-main').removeClass('col-md-9 col-lg-9').addClass('col-md-12 col-lg-12');
    $('.me-affix button').attr('title', '显示边栏');
    $('.me-affix input[name=next-status]').val('');
  }

  var nextFa = $('.me-affix input[name=next-fa]').val();
  if (nextFa == 'fa-angle-double-left') {
    $('.me-affix button > i').removeClass('fa-angle-double-right').addClass('fa-angle-double-left');
    $('.me-affix input[name=next-fa]').val('fa-angle-double-right');
  } else {
    $('.me-affix button > i').removeClass('fa-angle-double-left').addClass('fa-angle-double-right');
    $('.me-affix input[name=next-fa]').val('fa-angle-double-left');
  }
}

function formControlError() {
  var control = arguments[0];
  var msg = "";
  if (arguments.length > 1) msg = arguments[1];
  $(control).removeClass("has-success has-warning").addClass("has-error");
  $(control + " .help-block").html(msg);
}

/**
 * @param control div
 * @param msg 可选
 */
function formControlSuccess() {
  var control = arguments[0];
  var msg = "";
  if (arguments.length > 1) msg = arguments[1];
  $(control).removeClass("has-error has-warning").addClass("has-success");
  $(control + " .help-block").html(msg);
}

/**
 * @param control div
 * @param msg 可选
 */
function formControlWarning() {
  var control = arguments[0];
  var msg = "";
  if (arguments.length > 1) msg = arguments[1];
  $(control).removeClass("has-error has-success").addClass("has-warning");
  $(control + " .help-block").html(msg);
}

/**
 * @param control div
 * @param msg 可选
 */
function formControlInfo() {
  var control = arguments[0];
  var msg = "";
  if (arguments.length > 1) msg = arguments[1];
  $(control).removeClass("has-error has-success has-warning");
  $(control + " .help-block").html(msg);
}

function formControlClear(control) {
  $(control).removeClass('has-error has-warning has-success');
  $(control + " textarea").val('').html('');
  $(control + " input").val('');
  $(control + " select option").removeAttr('selected');
  $(control + " .help-block").html('');
}

function formControlDisabled() {
  var len = arguments.length;

  if (len == 0) {
    $("fieldset").attr("disabled", "disabled");
  } else {
    var exp = '';
    var i = 0;
    while (i < len) {
      exp = arguments[i]
      $(exp + " fieldset").attr("disabled", "disabled");
      i = i + 1;
    }
  }
}

function formControlRemove() {
  var len = arguments.length;

  if (len == 0) {
    $(".form-actions").remove();
    $(".edit-actions").remove();
  } else {
    var exp = '';
    var i = 0;
    while (i < len) {
      exp = arguments[i]
      $(exp + " .form-actions").remove();
      $(exp + " .edit-actions").remove();
      i = i + 1;
    }
  }
}

function editorForMarkdown(control, editor, preview) {
  $('a[href="' + preview + '"]').on('show.bs.tab',
    function (e) {
      $.post("/api/document/markdown/parser",
        {
          content: $(editor + ' textarea').val()
        },
        function (data, status) {
          if (status == 'success') $(preview + ' > div').html(data);
          else formControlError(control, "文档解析Markdown失败");
        });
    });
}

$(function () {
  $('.me-affix button').click(setupPageAffix);
})
